#!/usr/bin/perl -w

# Copyright (C) 2010 Modestas Vainius <modax@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

=head1 NAME

dh_sodeps - generate library dependencies for development *.so symlinks

=head1 SYNOPSIS

B<dh_sodeps> [S<I<debhelper options>>] [B<-V>I<versioninfo>]

=head1 DESCRIPTION

dh_sodeps is a helper program that generates library dependencies for *.so
symlinks that are typically found in the library development packages.

It basically looks for F<usr/lib/*.so> in the package build directory, finds
all local arch specific packages that actually contain targets of those
symlinks and adds dependencies on the discovered packages to the C<so:Depends>
substitution variable. Dependencies are strict by default, i.e.
(=${binary:Version}) if the package containing *.so is arch specific or
(>=${source:Version}) if it is arch independent.

=head1 OPTIONS

=over 4

=item B<-V>I<versioninfo>, B<--version-info>=I<versioninfo>

Use the specified version information for dependencies. If this option is not
specified, dh_sodeps will generate a strict dependency as explained above.
Please note that you don't need to enclose the value in brackets. dh_sodeps
will do this automatically.

=item B<-X>I<item>, B<--exclude> I<item>

Do not calculate dependencies for *.so files that contain "item" anywhere in
their path/filename. You may use this option multiple times to build up a list
of things to exclude.

=back

=cut

use strict;
use warnings;

use Debian::Debhelper::Dh_Lib;

my $opt_verinfo;

init(options => {
    'version-info|V' => \$opt_verinfo,
});

exit 0 unless @{$dh{DOPACKAGES}};

my @packages = getpackages("arch");

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $tmpdir = tmpdir($package);
    my $globstring = "$tmpdir/usr/lib/*.so";
    my $multiarch = dpkg_architecture_value("DEB_HOST_MULTIARCH");
    if (defined $multiarch) {
        $globstring = "$globstring $tmpdir/usr/lib/$multiarch/*.so"
    }
    my @solinks = grep { -l $_ } glob($globstring);

    if (@solinks) {
        my $arch = package_arch($package);
        my $verinfo;

        if (defined $opt_verinfo) {
            $verinfo = $opt_verinfo;
        } elsif ($arch eq "all") {
            $verinfo = '>= ${source:Version}';
        } else {
            $verinfo = '= ${binary:Version}';
        }

        foreach my $solink (@solinks) {
            next if excludefile($solink);

            my $target = readlink($solink);
            foreach my $p (@packages) {
                next if $p eq $package;
                if (-e "debian/$p/usr/lib/$target" || (defined $multiarch and -e "debian/$p/usr/lib/$multiarch/$target")) {
                    addsubstvar($package, "so:Depends", $p, $verinfo);
                }
            }
        }
    }
}

exit 0;

=head1 SEE ALSO

L<debhelper(7)>

=head1 AUTHOR

Modestas Vainius <modax@debian.org>

=cut
